<?php 
if(!function_exists('html_entity_decode_utf8')) {
	function html_entity_decode_utf8($string) {
		static $trans_tbl;
		
		// replace numeric entities
		$string = preg_replace('~&#x([0-9a-f]+);~ei', 'code2utf(hexdec("\\1"))', $string);
		$string = preg_replace('~&#([0-9]+);~e', 'code2utf(\\1)', $string);

		// replace literal entities
		if (!isset($trans_tbl)) {
			$trans_tbl = array();
			
			foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
				$trans_tbl[$key] = utf8_encode($val);
		}
		
		return strtr($string, $trans_tbl);
	}
}

if(!function_exists('getGMDate')) {
	/**
	 * function get GMT date
	 *
	 * @author ducnguyen1504@gmail.com
	 * @copyright 2011 by ducnguyen1504@gmail.com
	 *
	 * @param string $format. Ex: 'd/m/Y', 'd/m/Y H:i:s',... // string format is standard of php
	 * @param number $time. $time is timestamp.
	 * @param number $time_zone 
	 * @return string date
	 */
	function getGMDate($format='', $time_zone='', $time='') {
		if($format == '') $format = 'd/m/Y H:i:s'; // Vietnam is date format
		if($time_zone == '' OR is_string($time_zone)) $time_zone = 7; // (GMT +7:00) Vietnam
		if(is_string($time)) $time = strtotime($time); // convert to timestamp
		if(is_null($time)) $time = time(); // default is now
		
		return gmdate($format, $time + 3600 * $time_zone);
	}
}

if(!function_exists('isJapanese')) {
	function isJapanese($word) {
    return preg_match('/[\x{4E00}-\x{9FBF}\x{3040}-\x{309F}\x{30A0}-\x{30FF}]/u', $word);
	}	
}

	/**
	 * Paging function
	 *
	 * @param integer $total 			is total record
	 * @param integer $per_page 	is num row per page
	 * @param integer $page 			is current page 
	 * @param string $url					link to paging
	 * @param integer $num_show		num link page display
	 *
	 * @return array :
	 *					String $paging display link page
	 *					Integer $offset record start of current page length $per_page
	 */
	function paging($total, $per_page=5, $page=1, $url='', $num_show=5) {
		$num_page = floor($total / $per_page);
		if(($total % $per_page) > 0 ) $num_page++;
		
		$offset = ($page * $per_page) - $per_page;
		
		$url = (strpos($url, '?') > -1) ? $url . '&page=' : $url . '?page=';
		$prev = '<span class="paging_disable">Previous</span>';
		$first = '<span class="paging_disable">First</span>';
		if($page > 1) {
			$prev = '<a class="paging_link" href="' . $url . ($page - 1) . '">Previous</a>';
			$first = '<a class="paging_link" href="' . $url . '1">First</a>';
		}
		
		$next = '<span class="paging_disable">Next</span>';
		$last = '<span class="paging_disable">Last</span>';
		if($page < $num_page) {
			$next = '<a class="paging_link" href="' . $url . ($page + 1) . '">Next</a>';
			$last = '<a class="paging_link" href="' . $url . $num_page . '">Last</a>';
		}
		
		$link = '';
		$m = floor($num_show / 2);
		if($num_show % 2 == 0) $n = $m - 1;
		else $n = $m;
		
		$start = $page > $m ? $page - $m : 1;
		if($start + $num_show > $num_page) $start = $num_page - $num_show + 1;
		if($start <= 0 ) $start = 1;
		
		$end = $page + $n;
		if($end < $num_show) $end = $num_show;
		if($end > $num_page) $end = $num_page;
		
		for($i = $start; $i <= $end; $i++) {
			if($i == $page) { 
				$link .= '<span class="current_link">' . $i . '</span>';
			} else {
				$link .= '<a class="paging_link" href="' . $url . $i . '">' . $i . '</a>';
			}
		}
		
		$paging = $first . $prev . $link . $next . $last;
		return array('paging'=>$paging, 'offset'=>$offset);
	}

	/**
	 * Paging function
	 *
	 * @param integer $total 			is total record
	 * @param integer $per_page 	is num row per page
	 * @param integer $page 			is current page 
	 * @param string $url					link to paging
	 * @param integer $num_show		num link page display
	 *
	 * @return array :
	 *					String $paging display link page
	 *					Integer $offset record start of current page length $per_page
	 */
	function pagingAjax($total, $per_page=5, $page=1, $funcName='', $num_show=5) {
		$num_page = floor($total / $per_page);
		if(($total % $per_page) > 0 ) $num_page++;
		
		$offset = ($page * $per_page) - $per_page;
		
		$prev = '<span class="paging_disable">Previous</span> ';
		$first = '<span class="paging_disable">First</span> ';
		if($page > 1) {
			$prev = '<a class="paging_link" href="javascript:' . $funcName ."(".($page - 1). ')">Previous</a> ';
			$first = '<a class="paging_link" href="javascript:' . $funcName ."(1)".'">First</a> ';
		}
		
		$next = '<span class="paging_disable">Next</span> ';
		$last = '<span class="paging_disable">Last</span> ';
		if($page < $num_page) {
			$next = '<a class="paging_link" href="javascript:' . $funcName ."(". ($page + 1) .')">Next</a> ';
			$last = '<a class="paging_link" href="javascript:' . $funcName ."(". $num_page .')">Last</a> ';
		}
		
		$link = '';
		$m = floor($num_show / 2);
		if($num_show % 2 == 0) $n = $m - 1;
		else $n = $m;
		
		$start = $page > $m ? $page - $m : 1;
		if($start + $num_show > $num_page) $start = $num_page - $num_show + 1;
		if($start <= 0 ) $start = 1;
		
		$end = $page + $n;
		if($end < $num_show) $end = $num_show;
		if($end > $num_page) $end = $num_page;
		
		for($i = $start; $i <= $end; $i++) {
			if($i == $page) { 
				$link .= '<span class="current_link">' . $i . '</span> ';
			} else {
				$link .= '<a class="paging_link" href="javascript:' . $funcName ."(" . $i .')">' . $i . '</a> ';
			}
		}
		
		$paging = $first . $prev . $link . $next . $last;
		return array('paging'=>$paging, 'offset'=>$offset);
	}

	/* Generate a HTML Table upon an array
	// $arrHeader: list of column name (optional)
	// $arrData: content to load row data (require)
	// $title: Title on the top of table (optional)
	// $total: full record of query result (require)
	// $numPerPage: total showing records per page (require)
	// $transferType: submit, ajax (optional)
	// $tableId: table id (optional)
	// $sortColumn: show sort button
	// depedent module : common_functions.php
	//                   theme.css
	*/
	function genTableHTML($arrHeader, $arrData, $title, $total, $numPerPage, $funcNameOrUrl, $transferType="ajax", $tableId, $sortColumn){		
		// calculate AjaxPaging
		if ($transferType=="submit"){// submit type
			$paging = paging($total, $numPerPage=5, $page=1, $funcNameOrUrl, $num_show=5);
			$funcNameOrUrl."&pg=-1";
		}else if ($transferType=="ajax"){// ajax type
			$paging = pagingAjax($total, $numPerPage=5, $page=1, $funcNameOrUrl, $num_show=5);
			$funcNameOrUrl = $funcNameOrUrl.'(-1)';
		}
		$numOfCol = count($arrData[0])/2;
		$strPaging = $paging["paging"];
		$strPaging = '<a href="javascript:'.$funcNameOrUrl.'" class="paging_link">View All</a> '.$strPaging;
		$strHTML  = '<div style="font-family:sans serif">'; // [load div round]		
		$strHTML .= '<table id="'.$tableId.'" class="dyna_table tablesorter">'; // [dynamic table]
		$strHTML .= "<thead>";
		$strHTML .= "<tr>"; // [load title and paging]
		$strHTML .= '<td class="header" colspan="'.$numOfCol.'" style="background-color:#666666">';
		$strHTML .= '<div style="float:left"><b><font size="2" >'.strtoupper($title).'</font></b></div>';
		$strHTML .= '<div style="float:right">'.$strPaging.'</div>';
		$strHTML .= '</td>';
		$strHTML .= "</tr>"; // end[load title and paging]
		$strHTML .= '<tr style="background-color:#808080">';
		for($i=0;$i<$numOfCol;$i++){
			// use field name instead if doesn't exist column name			
			$strHTML .= '<th class="header">'.(isset($arrHeader[$i])?$arrHeader[$i]:"&nbsp;");			
			$strHTML .= "</th>";			
		}
		$strHTML .= "</tr>";
		$strHTML .= "</thead>";
		$strHTML .= "<tbody>";
		$intCountData = count($arrData);
		for($i=0;$i<$intCountData;$i++){
			$strHTML .= '<tr>';
			for($k=0;$k<$numOfCol;$k++){				
				$class=($k==0)?"first":($k==($numOfCol-1)?"last":"middle");
				// if ($i==($intCountData-1)){
					// $class="";
					// if ($k==0){
						// $class ="firstrowcell";
					// }
					// elseif($k==($numOfCol-1)){
						// $class ="lastrowcell";
					// }
				// }
				$strHTML .= '<td class="'.$class.'">'.$arrData[$i][$k]."</td>";
			}
			$strHTML .= "</tr>";	
		}
		$strHTML .= "</tbody>";
		$strHTML .= "</table>";// end[dynamic table]
		$strHTML .= "</div>";// end[load div round]
		// supply script to interactive more flexible
		// search script
		// sort script
		$strHTML .= '<script type="text/javascript">
			$(document).ready(function() 
				{ 
					$("#tbl1").tablesorter(); 
				} 
			); 
			function sort(tableid, intCol){
				//tableid = document.getElementById(tableid);
				tableid = "#" + tableid;
				// toggle sort status				
				if ($(tableid).attr("sort")=="down"){
					$(tableid).attr("sort","up");					
				}
				else{
					$(tableid).attr("sort","down"); 
				}
				// change icon up/down
				var tr = $(tableid).children().find("tr").children();
				var url = tr['.($sortColumn+1).'].children[0].src;
				var intSort=0;
				if (url.indexOf("sort_up.png")!==-1){
					tr['.($sortColumn+1).'].children[0].src=url.replace("sort_up","sort_down");					
					intSort = 0;
				}else{
					tr['.($sortColumn+1).'].children[0].src=url.replace("sort_down","sort_up");
					intSort = 1;
				}				
				// sort table by sortColumn
				
			}			
		</script>';// 
		return $strHTML;
	}
?>